package com.fancy.learncenter.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.WxPayDataBean;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.google.gson.Gson;
import com.superservice.lya.R;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fancy.hyypaysdk.pay.PayCallBack;
import fancy.hyypaysdk.pay.wxpay.WXPayUtil;

public class ChongZhiActivity extends BaseActivity {


    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.wx_icon)
    ImageView wxIcon;
    @Bind(R.id.person_icon)
    ImageView personIcon;
    @Bind(R.id.card_icon)
    ImageView cardIcon;
    @Bind(R.id.card_pws)
    EditText cardPws;
    @Bind(R.id.payNum)
    TextView payNum;
    @Bind(R.id.tv_10)
    TextView tv10;
    @Bind(R.id.tv_50)
    TextView tv50;
    @Bind(R.id.tv_100)
    TextView tv100;
    @Bind(R.id.tv_300)
    TextView tv300;
    @Bind(R.id.tv_500)
    TextView tv500;
    @Bind(R.id.money_tv)
    EditText moneyTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chongzhi, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("充值");

        moneyTv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                payNum.setText(s.toString());
            }
        });
    }

    @OnClick({R.id.wx_ll, R.id.card_ll, R.id.person_ll, R.id.pay, R.id.tv_10, R.id.tv_50, R.id.tv_100, R.id.tv_300, R.id.tv_500, R.id.money_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wx_ll:
                break;
            case R.id.card_ll:
                break;
            case R.id.person_ll:
                break;
            case R.id.pay:
                wxPay(indexMoney);
                break;
            case R.id.tv_10:
                initMoney(10);
                break;
            case R.id.tv_50:
                initMoney(50);
                break;
            case R.id.tv_100:
                initMoney(100);
                break;
            case R.id.tv_300:
                initMoney(300);
                break;
            case R.id.tv_500:
                initMoney(500);
                break;
            case R.id.money_tv:
                initMoney(0);
                break;
        }
    }

    private int indexMoney = 10;

    private void initMoney(int index) {
        indexMoney = index;
        moneyTv.setBackgroundResource(R.drawable.shape_line_gray);
        tv10.setBackgroundResource(R.drawable.shape_line_gray);
        tv50.setBackgroundResource(R.drawable.shape_line_gray);
        tv100.setBackgroundResource(R.drawable.shape_line_gray);
        tv300.setBackgroundResource(R.drawable.shape_line_gray);
        tv500.setBackgroundResource(R.drawable.shape_line_gray);
        switch (index) {
            case 10:
                payNum.setText("10");
                tv10.setBackgroundResource(R.drawable.shape_line_orange);
                break;
            case 50:
                payNum.setText("50");
                tv50.setBackgroundResource(R.drawable.shape_line_orange);
                break;
            case 100:
                payNum.setText("100");
                tv100.setBackgroundResource(R.drawable.shape_line_orange);
                break;
            case 300:
                payNum.setText("300");
                tv300.setBackgroundResource(R.drawable.shape_line_orange);
                break;
            case 500:
                payNum.setText("500");
                tv500.setBackgroundResource(R.drawable.shape_line_orange);
                break;
            case 0:
                moneyTv.setBackgroundResource(R.drawable.shape_line_orange);
                break;
        }
    }

    //    类型:1余额,2微信,3充值卡
    private int index = 2;

    private void changeIcon(int index) {
        this.index = index;
        wxIcon.setImageResource(R.drawable.xrefresh_ok);
        personIcon.setImageResource(R.drawable.xrefresh_ok);
        cardIcon.setImageResource(R.drawable.xrefresh_ok);

        if (index == 1) {
            personIcon.setImageResource(R.mipmap.bg_gouxuan);
        } else if (index == 2) {
            wxIcon.setImageResource(R.mipmap.bg_gouxuan);
        } else if (index == 3) {
            cardIcon.setImageResource(R.mipmap.bg_gouxuan);
        }
    }


    private void submit() {

        Map maps = new HashMap();
        maps.put("type", index + "");
        maps.put("price", "98");
        maps.put("month", "12");
        maps.put("card", cardPws.getText().toString());

        HttpMehtod.getInstance().payYgt(maps, new IdeaObserver<BaseDataBean>(this, true) {

            @Override
            public void onSuccess(BaseDataBean stringBaseDataBean) {
                ToastUtil.show("充值成功");
                MyApplication.is_device = "1";
                finish();
            }

            @Override
            public void onFail(int errorCode) {

            }

        });

    }

    private void wxPay(int index) {
        LogUtil.MyLog("wxPay", "======wxPay===" + index);
        Map maps = new HashMap();
        switch (index) {
            case 10:
                maps.put("payMoney", "10");
                break;
            case 50:
                maps.put("payMoney", "50");
                break;
            case 100:
                maps.put("payMoney", "100");
                break;
            case 300:
                maps.put("payMoney", "300");
                break;
            case 500:
                maps.put("payMoney", "500");
                break;
            case 0:
                if (!TextUtils.isEmpty(moneyTv.getText().toString())) {
                    maps.put("payMoney", moneyTv.getText().toString());
                } else {
                    ToastUtil.show("最少充值一元");
                }

                break;
        }

        maps.put("payMoney", payNum.getText().toString());
        HttpMehtod.getInstance().wxPay(maps, new IdeaObserver<BaseDataBean<WxPayDataBean>>(this, true) {

            @Override
            public void onSuccess(BaseDataBean<WxPayDataBean> stringBaseDataBean) {
                if (stringBaseDataBean.getData() != null) {
                    Gson gson = new Gson();
                    String json = gson.toJson(stringBaseDataBean.getData());

                    WXPayUtil.getInstance().wxPay(ChongZhiActivity.this, json, new PayCallBack() {
                        @Override
                        public void paySuccess() {
                            ToastUtil.show("支付成功");
                            finish();
                        }

                        @Override
                        public void payFaild() {
                            ToastUtil.show("支付失败");
                        }

                        @Override
                        public void payCancle() {
                            ToastUtil.show("支付取消");
                        }
                    });
                }

            }

            @Override
            public void onFail(int errorCode) {

            }

        });
    }

}
